unit AppInit;

{$mode objfpc}{$H+}

//========================================================================================
//
// Unit : AppInit.pas
//
// Description : This module provides
//
// Called By : Main :TfrmMain.FormShow
//
// Calls : Configure : ReadConfigurationFile
//         Main
//         PersonelTypesTable : CreatePersonelTypesTable
//
// Ver. : 1.00
//
// Date : 22 Mar 2016
//
//========================================================================================

interface

uses
  Classes, Dialogs, Fileutil, SysUtils,
  // Application Units
  Configure, PersonelTypesTable;

function Initialize: boolean;

implementation

uses
  Main;

function Initialize: boolean;
  var
    vstrTStr : string;
begin

  ShowMessage ('Init');

    // Get the Application Path
  GetDir(0, vstrTStr);
  frmConfigure.strAppPath := vstrTStr;

    // Read the Cobnfiguration File. If it does not exist we create it using default data
    // elements
  frmConfigure.ReadConfigurationFile;

    // Check to see if this is a new installation. If it is New Installation there are
    // two possibilities.
    //
    // 1. It is a Virgin Installation and we have to create an empty Reports folder.
    //
    // 2. An Installation exists but the user has deleted the .cfg file to reset the
    //    default Application data elements.
  if frmConfigure.blnNewConfigurationFile then
  begin

    showmessage('New Settings');

    // It is a New .cfg file so we set the Default Data and Reports path
    //
    frmConfigure.strDataPath := frmConfigure.strAppPath + '\' +
                                   frmConfigure.cstrDefDataDirName;

    frmConfigure.strReportsPath := frmConfigure.strAppPath + '\' +
                                   frmConfigure.cstrDefReportsDirName;

    // The Reports Path has been set to the Default Reportspath, and if it does not exist
    // then this is a new installation so we have to create it.
    if not DirectoryExists (frmConfigure.strDataPath) then
    begin

      showmessage('Creating New Data Directory');

      if not CreateDir(frmConfigure.strDataPath) then
      begin
        showmessage('Unable to Create Data Directory');
        Result := False;
        Exit;
      end;// if not CreateDirectory(frmConfigure.strDataPath)

      showmessage(frmConfigure.strDataPath);

    end;// if not DirectoryExists (frmConfigure.strDataPath)

    if not DirectoryExists (frmConfigure.strReportsPath) then
    begin

      showmessage('Creating New Reports Directory');

      if not CreateDir(frmConfigure.strReportsPath) then
      begin
        showmessage('Unable to Create Reports Directory');
        Result := False;
        Exit;
      end;// if not CreateDirectory(frmConfigure.strReportsPath)

    end;// if not DirectoryExists (frmConfigure.strReportsPath)

  end;
  frmConfigure.blnNewConfigurationFile := False;

  Result := True;

end;// procedure Initialize

//========================================================================================
end.// unit AppInit

